//
//  ByNewsCenterVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/11.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByNewsCenterVC.h"
#import "ByCoreViewSegmentControl.h"
@interface ByNewsCenterVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ByCoreViewSegmentControl *segTitle;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ByNewsCenterVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"消息中心";
    
    [self initViews];
    [self loadData];
    
    self.type = 0;
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    if (self.type == 0) {
        netModel.read_status = @"0";
    }else{
        netModel.read_status = @"1";
    }
    [ByCoreToolAlert showLoadind:@"加载中"];
    [ByApiOther messageQuery:netModel success:^(ByNetBaseResult *result) {
        SLog(@"消息查询 = %@",result.data);
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.dataArray addObject:netModel];
        }
        
        [self.tableView reloadData];
    } failure:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert showLoadind:result.msg];
    }];
}


-(void)initViews{
    
    self.segTitle = [[ByCoreViewSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    [self.view addSubview:self.segTitle];
    [self _initData];
    
    
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
       
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(50);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(50);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    self.segTitle.sectionTitles = @[@"未读消息",@"已读消息"];
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = FONT(15);
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = [UIColor blackColor];
    self.segTitle.selectedTextColor = BY_THEME_COLOR;
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segTitle.selectionIndicatorHeight = 3;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        weakSelf.type = index;
        [weakSelf loadData];
    }];

    [self.view addSubview:self.segTitle];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        UILabel *line = [[UILabel alloc] init];
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell).offset(0);
            make.right.equalTo(cell).offset(0);
            make.bottom.equalTo(cell).offset(0);
            make.height.mas_equalTo(@1);
        }];
        line.backgroundColor = BY_LINE_COLOR;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [cell.contentView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    iconView.image = [UIImage imageNamed:@"guanyuwomen"];
    iconView.hidden = YES;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView).offset(16);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(@120);
        make.right.equalTo(cell.contentView).offset(-10);
    }];
    timeLabel.font = FONT(11);
    timeLabel.textColor = BY_THEME_COLOR;
    timeLabel.textAlignment = NSTextAlignmentRight;
    

    UILabel *titleLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.top.equalTo(cell.contentView).offset(13);
        make.height.mas_equalTo(@20);
        make.right.equalTo(timeLabel.mas_left).offset(-10);
    }];
    titleLabel.font = FONT(13);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    
    CGFloat content_height = [netModel.content getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 20, 1000) font:13].size.height + 10;
    UILabel *contentLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(0);
        make.top.equalTo(cell.contentView).offset(40);
        make.height.mas_equalTo(@(content_height));
        make.right.equalTo(cell.contentView).offset(-10);
    }];
    contentLabel.font = FONT(13);
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.numberOfLines = 0;
    
    titleLabel.text = netModel.title;
    contentLabel.text = netModel.content;
    timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:netModel.create_time.integerValue]];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    CGFloat content_height = [netModel.content getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 20, 1000) font:13].size.height + 10;
    return 50 + content_height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
